package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.mapper.ErpProductArea64Mapper;
import com.erp.gather.service.IErpProductArea64Service;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpProductArea64;
import com.ruoyi.gather.api.domain.bo.ErpProductArea64Bo;
import com.ruoyi.gather.api.domain.vo.ErpProductArea64Vo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 商品区域国家Service业务层处理
 *
 * @author erp
 * @date 2023-03-14
 */
@RequiredArgsConstructor
@Service
public class ErpProductArea64ServiceImpl implements IErpProductArea64Service {
    private final ErpProductArea64Mapper baseMapper;


    /**
     * 查询商品区域国家
     */
    @Override
    public ErpProductArea64Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品区域国家列表
     */
    @Override
    public TableDataInfo<ErpProductArea64Vo> queryPageList(ErpProductArea64Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpProductArea64> lqw = buildQueryWrapper(bo);
        Page<ErpProductArea64Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品区域国家列表
     */
    @Override
    public List<ErpProductArea64Vo> queryList(ErpProductArea64Bo bo) {
        LambdaQueryWrapper<ErpProductArea64> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpProductArea64> buildQueryWrapper(ErpProductArea64Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpProductArea64> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getAreaName()), ErpProductArea64::getAreaName, bo.getAreaName());
        lqw.eq(StringUtils.isNotBlank(bo.getAreaNo()), ErpProductArea64::getAreaNo, bo.getAreaNo());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpProductArea64::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增商品区域国家
     */
    @Override
    public Boolean insertByBo(ErpProductArea64Bo bo) {
        ErpProductArea64 add = BeanUtil.toBean(bo, ErpProductArea64.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品区域国家
     */
    @Override
    public Boolean updateByBo(ErpProductArea64Bo bo) {
        ErpProductArea64 update = BeanUtil.toBean(bo, ErpProductArea64.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpProductArea64 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品区域国家
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
